<?php

namespace App\Recharge\Api;

use App\Models\Admin\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Tiantianyuntong extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $data = [
            'cid'       => $bean->getSupplierNum(),
            'gdid'      => $bean->getGoodsId(),
            'accounts'  => $bean->getAccount(),
            'amount'    => $bean->getRechargeNum() * $bean->getStartCharging(), //充值数量
            'nurl'      => $bean->getNotifyUrl(),
            'osn'       => $bean->getOrderNo(),//订单号
            'cip'       => $bean->getIp(),
        ];
        $data['sign'] = md5(
            $data['cid'] . $data['osn'] . $data['gdid'] . $data['accounts'] . $data['amount'] . $bean->getSecretKey()
        );
        return $data;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('ecode', '');
        $order_sn = $request->input('osn', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, ['1002', '1003']))->setRechargeSuccess($status == '1002');
    }

    /**
     * @return mixed
     */
    public function apiNotifySuccess(): mixed
    {
        return response('success');
    }
}
